package com.ts.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.ts.comm.SysJson;
import com.ts.comm.SysNumber;
import com.ts.comm.SysResult;
import com.ts.comm.SysString;
import com.ts.constant.OpType;
import com.ts.system.dao.UserDao;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 马俊鹏 on 2017/5/17.
 */
@Controller
public class UserController {
	@Resource
	private UserDao userDao;

	/**
	 * 查询用户信息
	 * */
	@RequestMapping(value = "/user/userList",method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions(value = {"Y04:"+ OpType.View})
	public SysResult queryData(HttpServletRequest req,@RequestBody String code){
		String sWhere= SysString.toString(req.getParameter("where"));
		int pageNumber = SysNumber.toInt(req.getParameter("pageNumber"));
		int pageSize = SysNumber.toInt(req.getParameter("pageSize"));
		SysResult sr = userDao.query(sWhere,pageSize,pageNumber);
		return sr;
	}

	/**
	 * 查询用户信息
	 * */
	@RequestMapping(value = "/user/getUserInfo",method = RequestMethod.POST)
	@ResponseBody
	public SysResult getUserInfo (HttpServletRequest req, @RequestBody String code){
		HashMap<String,Object> map = SysJson.toHashMap(code);
		SysResult sr = userDao.getUserInfo((String) map.get("fcode"));
		return sr;
	}

	/**
	 * 添加用户信息
	 * @param request
	 * @param response
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/user/addUser",method = RequestMethod.POST)
	@ResponseBody
	public SysResult addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody String code){
		HashMap<String,Object> map = SysJson.toHashMap(code);

		SysResult sysResult = userDao.add(map);
		return sysResult;
	}

	/**
	 * 修改用户
	 * @param request
	 * @param response
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/user/editUser",method = RequestMethod.POST)
	@ResponseBody
	public SysResult editUser(HttpServletRequest request, HttpServletResponse response, @RequestBody String code){
		HashMap<String,Object> map = SysJson.toHashMap(code);
		SysResult sysResult = userDao.update(map, (String) map.get("fCode"));
		return sysResult;
	}

	/**
	 * 删除用户
	 * @param request
	 * @param response
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/user/deleteUser",method = RequestMethod.POST)
	@ResponseBody
	public SysResult deleteUser(HttpServletRequest request, HttpServletResponse response, @RequestBody String code){
		List list =  SysJson.toList(code,String.class);
		SysResult sysResult = userDao.deleteUser(list);
		return sysResult;
	}

	/**
	 * 查询用户信息
	 * */
	@RequestMapping(value = "/user/getDeptPost",method = RequestMethod.POST)
	@ResponseBody
	public SysResult getDeptPost(HttpServletRequest req){
		String usercode= SysString.toString(req.getParameter("usercode"));
		SysResult sysResult = userDao.getDeptPost(usercode);
		return sysResult;
	}

	@RequestMapping(value = "/user/getUserPostList", method = RequestMethod.POST)
	@ResponseBody
	public SysResult getUserPostList(HttpServletRequest req,@RequestBody String code){
		HashMap<String,Object> map = SysJson.toHashMap(code);
		SysResult sysResult = userDao.getUserPostList((String)map.get("usercode"));
		return sysResult;
	}

	@RequestMapping(value = "user/setUserPost", method = RequestMethod.POST)
	@ResponseBody
	public SysResult setUserPost(HttpServletRequest req,@RequestBody String code){
		HashMap<String,Object> map = SysJson.toHashMap(code);
		String usercode = (String) map.get("usercode");
		List<String> postCodeList = SysJson.toList((JSONArray) map.get("postCodeArray"),String.class);
		SysResult sysResult = userDao.setUserPost(postCodeList,usercode);
		return  sysResult;
	}
}
